<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/
 
 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($mediator, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <link href="default.css" rel="stylesheet" type="text/css" />
 <title>New Mediator</title>
 </head>
 <body>
 <div id="fulladmin">
 <div id='adminleft'>
 <p><center><b>Add New Mediator</b></center></p>
 </div>
 <div id='adminright'>
 <center><h1>Mediators</h1><br>
 <table border='0' cellpadding='10'>
   <tr><th>
 <?php
session_start();
//If your session isn't valid, it returns you to the login screen for protection
if(empty($_SESSION['myusername'])){
 header("location:main_login.php");
}
//echo $_SESSION['myusername'];
?>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
 
 <form action="" method="post">
 <div>
 <strong>Mediator Name: *</strong> <input type="text" name="mediator" value="<?php echo $mediator; ?>" /><br/>
 <br>
 <input type="submit" name="submit" value="Submit">
 <input type="submit" name="back" value="Back">
    </th></tr></table>
 </div>
 </form> 
 </div>
 <div id="adminright"><center><br><br><br><br>Return to main <a href="dashboard.php"><font color="red">Dashboard</font></a>, or you can <a href="log_out.php?id=<?php echo $_SESSION['userid']; ?>"><font color="red">Log Out</font></a></center></div>

 </body>
 </html>
 <?php 
 }
 
 
 

 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
	$mediator= mysql_real_escape_string(htmlspecialchars($_POST['mediator']));
 
	// check to make sure both fields are entered
	if (($mediator== ''))
	{
	// generate error message
		$error = 'ERROR: Please fill in all required fields!';
 
	// if either field is blank, display the form again
		renderForm($mediator, $error);
	}
	else
	{
	// save the data to the database
		mysql_query("INSERT mediator SET mediatorName='$mediator'")
		or die(mysql_error()); 
 
	// once saved, redirect back to the view page
		header("Location: mediator.php"); 
	}
 }
 else
 // if the form hasn't been submitted, display the form
 {
    if(isset($_POST['back']))
	   header("Location: mediator.php");
	else
	   renderForm('','');
 }
?> 